import 'dart:math';

import 'package:kq_flutter_widgets/widgets/listener/object_watcher/watcher.dart';
import 'package:kq_flutter_widgets/widgets/listener/object_watcher/watcher_callback.dart';

///可观察对象，
///[T]可观察的对象类型。
///例如：传入的是String，则会持有String对象，
///并可以设置[watcher]观察绑定的String对象值的变化。
class WatchableObject<T> {
  T? _watchableObject;
  double? _uuid;

  ///初始值
  WatchableObject({T? init}) {
    setValue(init);
  }

  ///设置值
  void setValue(T? other) {
    _watchableObject = Watcher().value(getUuid(), other);
  }

  ///设置观察者
  void watcher(WatcherCallback watcherCallback) {
    Watcher().watcher(getUuid(), watcherCallback);
  }

  ///获取值
  T? value() {
    return _watchableObject;
  }

  ///获取uuid
  double getUuid() {
    return _uuid ??= Random().nextDouble();
  }
}
